<template>
  <Spin :spinning="loading" tip="Loading...">
  <div>
    <!-- 搜索 -->
    <!-- 索赔费用调整--车厂 -->
    <TopFilter ref="TopFilterss" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="120" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable
      ref="table"
      :columns="columns"
      :params="fetchParams"
      :fetchapi="fetchapi"
      :datakey="'records'"
      :onColumnsChange="columns => (this.columns = columns)"
      :onRowSelectChange="selectChange"
      :is-select-column="false"
      columnsRef="ClaimExpensesChangelgr"
    >
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" :click="exportFileBtn.bind()">导出</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleAdd" :title="'预授权新增'" destroyOnClose width="1200px" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
    </Drawer>
    <BaseDialog :visible.sync="selectDelearFlag" :title="'经销商选择'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <DealerList @close="Delearclose" @save="Delearsave"></DealerList>
    </BaseDialog>
    <BaseDialog :visible.sync="visible" :title="'车型新增'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <vehicle @close="close" @save="save"></vehicle>
    </BaseDialog>
    <Drawer :visible.sync="visibleEdit" :title="'索赔费用调整'" destroyOnClose width="100%" :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <editForm @close="editclose" @save="editsave" :rowData="rowData"></editForm>
    </Drawer>
  </div>
  </Spin>
</template>
<script>
import { getListByPage, newSave, getListForExport, getclaimType,exporturl } from '@/api/claim/factory/ClaimExpensesChange/index';
import vehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
import DealerList from '@/pages/claimDemage/tools/DealerList.vue';
import editForm from './editForm.vue';
import { getPartType,getClaim } from '@/api/dictionary/index';
import { getAppId } from '@/assets/js/auth';
import { exportExcelFile } from '@/utils/index';
import { dictionary } from '@/utils/dictMixin';
import moment from "moment";
export default {
   mixins: [dictionary],
  components: {
    vehicle,
    editForm,
    DealerList
  },
  data() {
    return {
      loading:false,
      dataList: [],
      visible: false,
      visibleEdit: false,
      visibleDrawer: false,
      selectDelearFlag: false,
      collapse: true,
      infoId: null,
      fetchapi: getListByPage,
      topFilterInit: this.topFilterInitFun(),
      flterValue: {}, //参数
      rowData: {},
      columns: this.creatHeader(),
      fetchParams: {
        claimStatus: '55131003',
        xhrAbort: true
      }, //ipa 接口
      selectedRows: [], //选中经销商
      dialogTitle: '选择车辆',
      visibleAdd: false
    };
  },
  created() {
    if (getAppId() == 'arcfox') {
      this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543',[55431001])
    }else{
      this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList =this.createDictList('5543')
    }
    this.fetchParams.beginSubmitAt = moment().startOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.fetchParams.endSubmitAt = moment().endOf('week').format('YYYY-MM-DD HH:mm:ss');
    this.claimType();
    this.carType();
  },
  methods: {
    // 导出
    async exportFileBtn() {
      let json = Object.assign({}, { CODE: 'CODE_00000141' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }
      // let beginSubmitAt = this.topFilterInit.find(x => x.fieldName == "beginSubmitAt|endSubmitAt").initialValue[0];
      // let endSubmitAt = this.topFilterInit.find(x => x.fieldName == "beginSubmitAt|endSubmitAt").initialValue[1];
      // if(!beginSubmitAt) {
      //   this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
      //   return
      // }
      // if(!endSubmitAt) {
      //   this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
      //   return
      // }
      // if(getAppId() == 'arcfox') {
      //   if (moment(moment(endSubmitAt).format('YYYY-MM-DD')).diff(moment(beginSubmitAt), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return
      //   }
      // } else {
      //   if (moment(moment(endSubmitAt).format('YYYY-MM-DD')).diff(moment(beginSubmitAt), 'months', true) > 12) {
      //     this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
      //     return
      //   }
      // }
      // let obj = Object.assign({}, this.fetchParams, { claimStatus: 55131003,xhrAbort: false });
      // obj.beginSubmitAt = this.topFilterInit.find(x => x.fieldName == "beginSubmitAt|endSubmitAt").initialValue[0];
      // obj.endSubmitAt = this.topFilterInit.find(x => x.fieldName == "beginSubmitAt|endSubmitAt").initialValue[1];
      // this.loading = true;
      // getListForExport(obj).then(res => {
      //   exportExcelFile(res.data, '索赔费用调整导出');
      //   this.loading = false;
      // });
    },
    //  索赔类型
    async claimType() {
      const res2 = await getClaim();
      if (res2.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = res2.data.map(y => ({ text: y.name, value: y.code }));
      }
      // const res = await getclaimType({ type: 5501 });
      // if (res.resultCode == 200) {
      //   this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'cmsClaimType').itemList = res.data.map(y => ({
      //     text: y.codeCnDesc,
      //     value: y.codeId
      //   }));
      // }
    },
    async carType() {
      // const res3 = await getPartType({ type: 5543 });
      // if (res3.resultCode == 200) {
      //
      //   if (getAppId() == 'arcfox') {
      //     delete res3.data[0];
      //   }
      //
      //   this.topFilterInit.find(x => x.fieldName == 'ifComm').initialValue = '';
      //   this.topFilterInit.find(x => x.fieldName == 'ifComm').itemList = res3.data.map(y => ({ text: y.codeCnDesc, value: y.codeId }));
      // }
    },
    //搜索数据
    topFilterInitFun() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.SelectDelear()} />
              </div>
            );
          }
        },
        { type: 'INPUT', label: '索赔单号', fieldName: 'claimNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '预授权单号', fieldName: 'authNo', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '维修工单号', fieldName: 'roNo', placeholder: '请输入', initialValue: '' },
        { type: 'SELECT', label: '索赔类型', fieldName: 'cmsClaimType', placeholder: '请选择', initialValue: '', itemList: [] },
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入', initialValue: '' },
        {
          type: 'RANGE_DATE',
          label: '提交日期',
          fieldName: 'beginSubmitAt|endSubmitAt',
          placeholder: '请输入',
          initialValue: [
            moment()
              .startOf('week')
              .format('YYYY-MM-DD HH:mm:ss'),
            moment()
              .endOf('week')
              .format('YYYY-MM-DD HH:mm:ss')
          ],rules: [{ required: true, message: '请选择日期', trigger: 'change' }]
        },
        {
          type: 'RANGE_DATE',
          label: '审核日期',
          fieldName: 'beginApplyAt|endApplyAt',
          placeholder: '请输入',
          initialValue: ''
        },
        { type: 'INPUT', label: '主损件名称', fieldName: 'partName', placeholder: '请输入', initialValue: '' },
        { type: 'INPUT', label: '主损件代码', fieldName: 'partCode', placeholder: '请输入', initialValue: '' },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelCode',
          placeholder: '请输入',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        },
        { type: 'SELECT', label: '动力形式', fieldName: 'ifComm', placeholder: '请选择动力形式...', initialValue: '',itemList: [] },
        { type: 'INPUT', label: '批量活动名称', fieldName: 'batchName', placeholder: '请输入', initialValue: '' },

      ];
    },
    //搜索获取数据
    changeHandle(val) {
      if(!val.beginSubmitAt) {
        this.$notify.warning({ title: '提示信息', message: '提交开始日期不可为空！' });
        return
      }
      if(!val.endSubmitAt) {
        this.$notify.warning({ title: '提示信息', message: '提交结束日期不可为空！' });
        return
      }
      if(getAppId() == 'arcfox') {
        if (moment(moment(val.endSubmitAt).format('YYYY-MM-DD')).diff(moment(val.beginSubmitAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return
        }
      } else {
        if (moment(moment(val.endSubmitAt).format('YYYY-MM-DD')).diff(moment(val.beginSubmitAt), 'months', true) > 12) {
          this.$notify.warning({ title: '提示信息', message: '提交日期跨度不可超过十二个月！' });
          return
        }
      }
      this.fetchParams = Object.assign({}, this.fetchParams, val, { claimStatus: 55131003,xhrAbort: false });
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        { title: '经销商代码', dataIndex: 'ownerCode', sorter: true },
        { title: '经销商名称', dataIndex: 'ownerName', sorter: true },
        { title: '索赔单状态', dataIndex: 'statusStr', sorter: true },
        { title: '索赔单号', dataIndex: 'claimNo', sorter: true },
        { title: '预授权单号', dataIndex: 'authNo', sorter: true },
        { title: '维修工单号', dataIndex: 'roNo', sorter: true },
        { title: 'VIN', dataIndex: 'vin', sorter: true },
        { title: '车牌号', dataIndex: 'licenseNo', sorter: true },
        { title: '车型名称', dataIndex: 'modelName', sorter: true },
        { title: '索赔类型', dataIndex: 'cmsClaimTypeStr', sorter: true },
        { title: '动力形式', dataIndex: 'ifCommStr', sorter: true },
        { title: '主损件代码', dataIndex: 'partCode', sorter: true },
        { title: '主损件名称', dataIndex: 'partName', sorter: true },
        { title: '供应商', dataIndex: 'supName', sorter: true },
        { title: '行驶里程', dataIndex: 'driveMile', sorter: true },
        { title: '故障现象代码', dataIndex: 'faultCode', sorter: true },
        { title: '故障现象名称', dataIndex: 'faultName', sorter: true },
        { title: '分类', dataIndex: 'frCode', sorter: true },
        { title: '原因代码名称', dataIndex: 'frName', sorter: true },
        { title: '批量活动名称', dataIndex: 'batchName', sorter: true },
        { title: '驳回次数', dataIndex: 'channelNum', sorter: true },
        { title: '是否一口价', dataIndex: 'ifPrice', sorter: true },
        { title: '一口价金额', dataIndex: 'priceAmount', sorter: true },
        { title: '索赔金额', dataIndex: 'amount', sorter: true ,
          render: props => {
            return <span>{props.row.priceAmount==0?props.row.sumAmount:props.row.priceAmount}</span>;
          }},
        // { title: '索赔金额', dataIndex: 'sumAmount', sorter: true },
        { title: '工时费', dataIndex: 'labourAmount', sorter: true },
        { title: '配件费', dataIndex: 'partAmount', sorter: true },
        { title: '附加费用', dataIndex: 'addAmount', sorter: true },
        { title: '最终审核备注', dataIndex: 'checkRemark', sorter: true },
        { title: '保修起始日期', dataIndex: 'repairBeginDate', sorter: true },
        { title: '生产日期', dataIndex: 'prodDate', sorter: true }, //,dateFormat: 'yyyy-MM-dd'
        { title: '销售日期', dataIndex: 'saleDate', sorter: true }, // ,dateFormat: 'yyyy-MM-dd'
        { title: '维修日期', dataIndex: 'serviceBeginDate', sorter: true }, // ,dateFormat: 'yyyy-MM-dd'
        { title: '提交时间', dataIndex: 'submitAt', sorter: true }, // ,dateFormat: 'yyyy-MM-dd'
        { title: '申请人', dataIndex: 'applyBy', sorter: true },
        { title: '审核时间', dataIndex: 'checkAt', sorter: true }, // ,dateFormat: 'yyyy-MM-dd'
        { title: '审核人', dataIndex: 'checkBy', sorter: true }
      ];
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    close() {
      this.visible = false;
    },
    chooseOrderNo() {
      this.visible = true;
    },
    save(obj) {
      this.topFilterInit.find(x => x.fieldName == 'modelCode').initialValue = obj[0].modelCode;
      this.visible = false;
    },
    add() {
      this.visibleAdd = true;
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    editclose() {
      this.visibleEdit = false;
    },
    edit(val) {
      this.rowData = val;
      this.visibleEdit = true;
    },
    editsave() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    }, //经销商选择页面
    SelectDelear() {
      this.selectDelearFlag = true;
    }, //关闭
    Delearclose() {
      this.selectDelearFlag = false;
    },
    //保存
    Delearsave(val) {
      this.topFilterInit.find(x => x.fieldName == 'ownerCode').initialValue = val[0].dealerCode;
      this.selectDelearFlag = false;
    }
  }
  // mounted() {
  //   this.BaseTable = this.$refs.listModal.$refs.pageTable;
  // }
};
</script>

<style lang="less" scoped></style>
